package cn.sdxx.qddata.controller;

import cn.sdxx.qddata.common.R;
import cn.sdxx.qddata.entity.District;
import cn.sdxx.qddata.entity.Joke;
import cn.sdxx.qddata.service.DistrictService;
import cn.sdxx.qddata.service.JokeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "笑话模块")
@Slf4j
@RestController
@RequestMapping("/joke")
public class JokeController {

    @Autowired
    private JokeService jokeService;


    @ApiOperation("随机获取num个笑话(免认证)")
    @GetMapping()
    public R getJokesRandom(@RequestParam(required = false,value="num" ,defaultValue = "3")Integer num) {
        List<Joke> jokes = jokeService.getJokesRandom(num);
        return R.success(jokes);
    }

    @ApiOperation("以title为条件分页获取笑话(免认证)")
    @GetMapping("/page")
    public R getJokesByPage(@RequestParam(required = false,value="pageNo" ,defaultValue = "1")int pageNo,
                            @RequestParam(required = false,value="pageSize" ,defaultValue = "5")int pageSize,
                            String title) {

        //分页构造器
        Page<Joke> pageInfo = new Page<>(pageNo, pageSize);
        //条件构造器
        LambdaQueryWrapper<Joke> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(title != null, Joke::getTitle, title);
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(Joke::getId);
        jokeService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

}
